package com.base.utils;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 功能描述:
 *
 * @Author: TianRay
 * @Date: 2021/3/12 16:48
 **/
public class GitUtils {

    private Git git;
    private static final String ADDRESS = "D:\\Program Data\\IDEA\\base";

    /**
     * @方法简介: 获取所有的版本号与版本号对应的时间戳
     */
    public ArrayList<HashMap<String, Object>> getGitVersion()
            throws GitAPIException, IOException {
        this.git = Git.open(new File(ADDRESS));
        Iterable<RevCommit> logIterable = this.git.log().call();
        // 获取所有版本号的迭代器
        Iterator<RevCommit> logIterator = logIterable.iterator();
        int row = 0;
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        while (logIterator.hasNext()) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            RevCommit commit = logIterator.next();
            // 提交时间
            Date commitDate = commit.getAuthorIdent().getWhen();
            // 提交人
            String commitPerson = commit.getAuthorIdent().getName() ;
            // 提交的版本号（之后根据这个版本号去获取对应的详情记录）
            String commitID = commit.getName();
            map.put("version", commitID);
            map.put("commitDate", commitDate);
            map.put("commitPerson", commitPerson);
            list.add(row, map);
            row++;
        }
        return list;
    }

    /**
     * @方法简介 :根据指定版本号获取版本号下面的详情记录
     *
     */
    @SuppressWarnings("resource")
    public HashMap<String, Object> getCommitLogList(String revision) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        // 通过git获取项目库
        Repository repository = this.git.getRepository();
        // 根据所需要查询的版本号查新ObjectId
        ObjectId objId = repository.resolve(revision);

        // 根据版本号获取指定的详细记录
        Iterable<RevCommit> allCommitsLater = git.log().add(objId).call();
        if (allCommitsLater == null) {
            throw new Exception("该提交版本下无当前查询用户的提交记录");
        }
        Iterator<RevCommit> iter = allCommitsLater.iterator();

        // 获取提交的日期,提交人和提交日志

        RevCommit commit = iter.next();
        Date commitDate = commit.getAuthorIdent().getWhen();
        map.put("commitDate", commitDate);
        String commitPerson = commit.getAuthorIdent().getName();
        map.put("commitPerson", commitPerson);
        String message = commit.getFullMessage();
        map.put("message", message);

        // 给存储库创建一个树的遍历器
        TreeWalk tw = new TreeWalk(repository);
        // 将当前commit的树放入树的遍历器中
        tw.addTree(commit.getTree());

        commit = iter.next();
        if (commit != null) {
            tw.addTree(commit.getTree());
        } else {
            return null;
        }

        tw.setRecursive(true);
        RenameDetector rd = new RenameDetector(repository);
        rd.addAll(DiffEntry.scan(tw));
        // 获取到详细记录结果集    在这里就能获取到一个版本号对应的所有提交记录（详情！！！）
        List<DiffEntry> list = rd.compute();

        map.put("list", list);
        return map;
    }

}
